import 'dart:io';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

class BasicVideoTest extends StatefulWidget {
  const BasicVideoTest({super.key});

  @override
  State<BasicVideoTest> createState() => _BasicVideoTestState();
}

class _BasicVideoTestState extends State<BasicVideoTest> {
  VideoPlayerController? _controller;
  bool _isLoading = false;
  String _status = '准备测试基础视频播放';

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  Future<void> _testHttpVideo() async {
    setState(() {
      _isLoading = true;
      _status = '正在初始化HTTP视频...';
    });

    try {
      // 清理之前的控制器
      _controller?.dispose();
      
      final httpUrl = 'http://localhost:18888/123.mp4';
      print('创建VideoPlayerController - HTTP: $httpUrl');
      
      _controller = VideoPlayerController.networkUrl(Uri.parse(httpUrl));
      
      print('开始初始化...');
      await _controller!.initialize();
      
      print('初始化完成，开始播放...');
      await _controller!.play();
      
      setState(() {
        _isLoading = false;
        _status = 'HTTP视频播放成功！';
      });

      print('HTTP视频播放成功');
    } catch (e) {
      print('HTTP视频播放失败: $e');
      setState(() {
        _isLoading = false;
        _status = 'HTTP视频播放失败: $e';
      });
    }
  }

  Future<void> _testLocalVideo() async {
    setState(() {
      _isLoading = true;
      _status = '正在初始化本地视频...';
    });

    try {
      // 清理之前的控制器
      _controller?.dispose();
      
      final localFile = File('/Users/slothbear/Pictures/壁纸_副本/123.mp4');
      print('创建VideoPlayerController - 本地: ${localFile.path}');
      
      _controller = VideoPlayerController.file(localFile);
      
      print('开始初始化...');
      await _controller!.initialize();
      
      print('初始化完成，开始播放...');
      await _controller!.play();
      
      setState(() {
        _isLoading = false;
        _status = '本地视频播放成功！';
      });

      print('本地视频播放成功');
    } catch (e) {
      print('本地视频播放失败: $e');
      setState(() {
        _isLoading = false;
        _status = '本地视频播放失败: $e';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.black,
        foregroundColor: Colors.white,
        title: const Text('基础视频测试'),
      ),
      body: Column(
        children: [
          // 状态显示
          Container(
            padding: const EdgeInsets.all(16),
            child: Text(
              _status,
              style: const TextStyle(color: Colors.white, fontSize: 16),
              textAlign: TextAlign.center,
            ),
          ),
          
          // 测试按钮
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                onPressed: _isLoading ? null : _testLocalVideo,
                child: const Text('测试本地文件'),
              ),
              ElevatedButton(
                onPressed: _isLoading ? null : _testHttpVideo,
                child: const Text('测试HTTP'),
              ),
            ],
          ),
          
          const SizedBox(height: 16),
          
          // 加载指示器
          if (_isLoading)
            const CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
            ),
          
          // 视频播放器
          Expanded(
            child: Container(
              color: Colors.black,
              child: Center(
                child: _controller != null && _controller!.value.isInitialized
                    ? AspectRatio(
                        aspectRatio: _controller!.value.aspectRatio,
                        child: VideoPlayer(_controller!),
                      )
                    : const Text(
                        '选择一个测试选项',
                        style: TextStyle(color: Colors.white54),
                      ),
              ),
            ),
          ),
          
          // 播放控制
          if (_controller != null && _controller!.value.isInitialized)
            Container(
              padding: const EdgeInsets.all(16),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  ElevatedButton(
                    onPressed: () => _controller!.play(),
                    child: const Text('播放'),
                  ),
                  ElevatedButton(
                    onPressed: () => _controller!.pause(),
                    child: const Text('暂停'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      _controller!.seekTo(Duration.zero);
                      _controller!.play();
                    },
                    child: const Text('重播'),
                  ),
                ],
              ),
            ),
        ],
      ),
    );
  }
}